(function($){
	
	$(document)
	.ready(function(){
					
		$('#px_albumSelect').change(function(){
			var album_id = $(':selected', this).val();
			var album_name = $(':selected', this).text();
			 $('#px_addUpdateInput').val('');
			if( album_id==0 ){
				$('#px_deleteAlbumBtn').hide();
				$('#px_addUpdateAlbumBtn').val('Add »');
			}
			else{
				$('#px_addUpdateInput').val(album_name);
				$('#px_deleteAlbumBtn').show();
				$('#px_addUpdateAlbumBtn').val('Update »');
				$('#px_albumNameHolder').val(album_name);
			};		
		});
		
		$('#px_addUpdateAlbumBtn').click(function(){
			var album_name = $('#px_addUpdateInput').val();
			var album_id = $('#px_albumSelect :selected').val();
			
			if( album_id == 0 && album_name == ''){
				return false;
			};
			
			
			$.ajax({
				url : baseUrl + 'doAlbum',
				data : {
					"album_name":album_name,
					"album_id":album_id,
					"_once" : _once
				},
				dataType: 'json',
				complete : function(){jQloaderAnimation.hide();},
				error : function(XMLHttpRequest, textStatus, errorThrown){
					var errMsg = "There was an ajax error with a status of " + textStatus + " and thrown error of " + errorThrown;
					showNoticeMessage('error',errMsg);
				},
				beforeSend : function(){jQloaderAnimation.show();},
				success : function(data){
					if( album_id == 0 ){
						showNoticeMessage('confirm',"Data Save!");
						$('#px_albumSelect').append('<option id="pxOption'+data.id+'" value="'+data.id+'">'+data.album_name+'</option>');
						$tr = $('<tr id="pxTrId'+data.id+'">').prependTo('#px_albumTable tbody')
						$tr
						.append('<td>'+data.id+'</td>')
						.append('<td><a href="admin.php?page=px_manageAlbum&amp;pxalbum_action=manage_images&amp;aid='+data.id+'">'+data.album_name+' (Edit Images)</a></td>')
						.append('<td><a href="admin.php?page=px_manageAlbum&amp;pxalbum_action=delete_album&amp;aid='+data.id+'" class="px_optDelete">Delete</a></td>')
						.css({ backgroundColor: "yellow" })
						.animate({ backgroundColor: "transparent" }, 1000);
					}
					else{
						$tr = $('#pxTrId'+data.id);
						$('#pxOption'+data.id).text(data.album_name);
						
						color = $tr.css('background-color');
						
						$tr
						.css({ backgroundColor: "yellow" })
						.animate({ backgroundColor: color }, 1000)
						.children(':nth-child(2)')
						.empty()
						.append('<a href="admin.php?page=px_manageAlbum&amp;aid='+data.id+'">'+data.album_name+'</a>');
					}
				}
			});
		});
	
	});
})(pxjQuery);